﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace Spore.Utils
{
   public class StringEncrypter
    {
        static string key_64 = "_jiuzhou";
        internal static string Key_64
        {
            get { return StringEncrypter.key_64; }
            set
            {
                if (value.Length > 8)
                { StringEncrypter.key_64 = value.Substring(0, 8); }
                else if (value.Length < 8)
                { StringEncrypter.key_64 = value + "abcdefgh".Substring(0, 8 - value.Length); }
            }
        }

        static string iv_64 = "_jiuzhou";
        internal static string Iv_64
        {
            get { return StringEncrypter.iv_64; }
            set
            {
                if (value.Length > 8)
                { StringEncrypter.iv_64 = value.Substring(0, 8); }
                else if (value.Length < 8)
                { StringEncrypter.iv_64 = value + "abcdefgh".Substring(0, 8 - value.Length); }
            }
        }

        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="data">待加密字符串</param>
        /// <returns>加密后字符串</returns>
        public static string Encode(string data)
        {
            string Out = null;
            byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(key_64);
            byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(iv_64);
            try
            {
                DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
                int i = cryptoProvider.KeySize;
                MemoryStream ms = new MemoryStream();
                CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);
                StreamWriter sw = new StreamWriter(cst);
                sw.Write(data);
                sw.Flush();
                cst.FlushFinalBlock();
                sw.Flush();

                Out = Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
                sw.Close();
                ms.Close();
                return Out;
            }
            catch(Exception ex)
            {
                throw ex;
            }

        }
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="data">待解密字符串</param>
        /// <returns>解密的字符串</returns>
        public static string Decode(string data)
        {
            string Out = null;
            byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(key_64);
            byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(iv_64);
            byte[] byEnc;
            try
            {
                byEnc = Convert.FromBase64String(data);
                DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
                MemoryStream ms = new MemoryStream(byEnc);
                CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);
                StreamReader sr = new StreamReader(cst);
                Out = sr.ReadToEnd();
                ms.Close();
                sr.Close();
                return Out;
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }
    }
}
